/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ec.cd.server;

import ec.cd.common.Networking;
import ec.cd.server.gui.GraphThread;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JFrame;

/**
 *
 * @author endri
 */
public class ServerBlocking {

    /**
     *
     */
    public static final int CALIBRATION_RUN = 0;
    /**
     *
     */
    public static final int DETECTION_RUN = 1;
    public static final int POWERSAVE_RUN = 2;

    /**
     *
     */
    public static class ConfigInfo {

        /**
         *
         */
        public int samplingRate;
        /**
         *
         */
        public int sendingRate;
        /**
         *
         */
        public int lengthOfTest;
        /**
         *
         */
        public float[] accRangesTorso; //min, max (x,y,z) (sitting), min, max (x,y,z) (walking), min, max (x,y,z) (running) 18 values total
        public float[] gyrRangesTorso; //min, max (x,y,z) (sitting), min, max (x,y,z) (walking), min, max (x,y,z) (running) 18 values total
        
        public float[] accRangesArm; //min, max (x,y,z) (sitting), min, max (x,y,z) (walking), min, max (x,y,z) (running) 18 values total
        public float[] gyrRangesArm; //min, max (x,y,z) (sitting), min, max (x,y,z) (walking), min, max (x,y,z) (running) 18 values total
        
        public float[] accRangesLeg; //min, max (x,y,z) (sitting), min, max (x,y,z) (walking), min, max (x,y,z) (running) 18 values total
        public float[] gyrRangesLeg; //min, max (x,y,z) (sitting), min, max (x,y,z) (walking), min, max (x,y,z) (running) 18 values total
    }
    private ServerSocket serverSocket;
    private int runningMode;
    private ConfigInfo configInfo;

    /**
     *
     * @param runningMode
     * @param configInfo
     */
    public ServerBlocking(int runningMode, ConfigInfo configInfo) {
        this.runningMode = runningMode;
        this.configInfo = configInfo;
        try {
            // Create the server socket. 
            serverSocket = new ServerSocket(1337);

        } catch (IOException ioe) {
            System.out.println("Could not create server socket at 1337. Quitting.");
            System.exit(-1);
        }
    }

    /**
     *
     * @throws IOException
     */
    public void startListening() throws IOException {
       
        while (true) {
            //wait for client to connect
            System.out.println("Waiting for clients to connect:");
            Socket clientSocket = serverSocket.accept();
            ClientHandleThread clientThread = new ClientHandleThread(clientSocket, runningMode, configInfo);
            clientThread.start();
        }
    }
}
